package com.marco.service.system.impl;

import com.marco.dao.system.HomePageVisitorDao;
import com.marco.domain.system.HomePageVisitor;
import com.marco.service.system.HomePageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * HomePageServiceImpl.java
 * Create on 2017/1/23
 * Copyright(c) JD Inc. All Rights Reserved.
 * <pre>
 * 功能说明：
 * </pre>
 */
@Service
@Transactional
public class HomePageServiceImpl implements HomePageService {
    private static final Logger logger = LoggerFactory.getLogger(HomePageService.class);

    private final HomePageVisitorDao visitorDao;

    @Autowired
    public HomePageServiceImpl(HomePageVisitorDao visitorDao) {
        this.visitorDao = visitorDao;
    }

    @Override
    public Map<String, Object> welcome(HttpServletRequest request) {
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("success", false);
        try {
            List<HomePageVisitor> list = visitorDao.selectAll();
            for (HomePageVisitor visitor : list) {
//                logger.info("ID {} USER {}", visitor.getId(), visitor.getUserName());
                res.put("user", visitor.getUserName());
            }
            Random random = new Random();
            HomePageVisitor visitor = new HomePageVisitor();
            visitor.setUserName("xiaomingke" + random.nextInt(1000));
            visitorDao.insert(visitor);
        } catch (Exception e) {
            throw new RuntimeException("查询访问者出现异常", e);
        }
        return res;
    }
}
